package com.dormitoryAdministratorSystem.ui.roommanage;

import com.dormitoryAdministratorSystem.entity.room.RoomBean;
import com.dormitoryAdministratorSystem.service.room.impl.RoomSerImpl;
import com.dormitoryAdministratorSystem.ui.MainFrame;
import com.dormitoryAdministratorSystem.util.BeansUtil;
import com.dormitoryAdministratorSystem.util.CheckFormatUtil;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoTxt;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.ArrayList;

public class RoomAddDialog extends JDialog {
	
	private LovoTxt roomAddressTxt;
	private LovoTxt buildingTypeTxt;//户型
	private LovoTxt capNumTxt;//可容纳人数
	private LovoComboBox<String> roomTypeComb;
	private LovoTxt rentPriceTxt;
	private LovoTxt payTypeTxt;
	private LovoTxt roomHostTxt;//房东
	private LovoTxt hostTelTxt;
	private LovoComboBox<String> roomStatusCmb;//房间状态
	private LovoDate rentDate;
	
	private LovoButton addBtn;
	private LovoButton cancelBtn;

	private RoomSerImpl rsi;
	
	
	public RoomAddDialog(MainFrame frame, RoomSerImpl rsi) {
		// TODO Auto-generated constructor stub
		super(frame,"添加房间",true);
		this.rsi = rsi;
		this.setBounds(350, 280, 520, 400);
		this.setLayout(null);
		this.init();

		this.setVisible(true);
	}

	private void init() {
		// TODO Auto-generated method stub
		this.roomAddressTxt = new LovoTxt("房间地址", 20, 20, this);
		this.buildingTypeTxt = new LovoTxt("房间户型", 270, 20, this);
		this.capNumTxt = new LovoTxt("可容纳人数", 20, 80, this);
		
		ArrayList<String> roomTypeLst = new ArrayList<String>();
		roomTypeLst.add("男生宿舍");
		roomTypeLst.add("女生宿舍");
		this.roomTypeComb = new LovoComboBox<String>( "房间类型",roomTypeLst, 270, 80, this);
		
		this.rentPriceTxt = new LovoTxt("房租", 20, 140, this);
		this.payTypeTxt = new LovoTxt("支付方式", 270, 140, this);
		this.roomHostTxt = new LovoTxt("房东", 20, 200, this);
		this.hostTelTxt = new LovoTxt("房东电话", 270, 200, this);
		
		ArrayList<String> roomStatusLst = new ArrayList<String>();
		roomStatusLst.add("正常");
		roomStatusLst.add("损坏");
		this.roomStatusCmb = new LovoComboBox<String>( "房间状态",roomStatusLst, 20, 260, this);
		
		this.rentDate = new LovoDate("租房日期", 270, 260, this);
		
		this.addBtn = new LovoButton("添加", 120, 320, this);
		this.cancelBtn = new LovoButton("取消", 270, 320, this);
		this.addBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				try {
					if ("".equals(roomAddressTxt.getText().trim()) || "".equals(capNumTxt.getText().trim())
							|| "".equals(rentPriceTxt.getText().trim()) || "".equals(roomHostTxt.getText().trim())
							|| "".equals(hostTelTxt.getText().trim()) || "".equals(buildingTypeTxt.getText().trim())
							|| "".equals(payTypeTxt.getText().trim())) {
						JOptionPane.showMessageDialog(null, "添加失败，不能有信息为空");
					} else if (!CheckFormatUtil.isPhoneNum(hostTelTxt.getText())) {
						JOptionPane.showMessageDialog(null, "添加失败，电话格式错误\n" +
								"必须是13,15,17,18,19开头的11位手机号");
					} else if (!CheckFormatUtil.isNum(capNumTxt.getText())) {
						JOptionPane.showMessageDialog(null, "添加失败，房间人数范围：4-29");
					} else if (!CheckFormatUtil.isDouble(rentPriceTxt.getText())) {
						JOptionPane.showMessageDialog(null, "添加失败，房租输入错误");
					}else if(!CheckFormatUtil.checkLength(roomAddressTxt.getText().trim(), 5, 30) ||
							 !CheckFormatUtil.checkLength(roomHostTxt.getText().trim(), 2, 10) ||
							 !CheckFormatUtil.checkLength(buildingTypeTxt.getText().trim(), 2, 10) ||
							!CheckFormatUtil.checkLength(payTypeTxt.getText().trim(), 2, 10)){

						JOptionPane.showMessageDialog(null,
								"添加失败\n" +
										"地址长度范围： 5-30\n" +
										"房东姓名长度范围：2-10\n" +
										"支付模式长度范围： 2-10\n" +
										"房间户型长度范围： 2-10");
					} else if(BeansUtil.getrMap().get(roomAddressTxt.getText().trim()) != null) {
						JOptionPane.showMessageDialog(null, "添加失败，房间地址重复");
					}else {
						RoomBean r = new RoomBean();
						r.setAddress(roomAddressTxt.getText().trim());
						r.setSize(Integer.parseInt(capNumTxt.getText().trim()));
						r.setRent(Double.parseDouble(rentPriceTxt.getText().trim()));
						r.setRoomState(roomStatusCmb.getItem());
						r.setHostName(roomHostTxt.getText().trim());
						r.setHostTel(hostTelTxt.getText().trim());
						r.setRoomType(roomTypeComb.getItem());
						r.setHouseType(buildingTypeTxt.getText().trim());
						r.setPayMode(payTypeTxt.getText().trim());
						r.setRentTime(new Date(rentDate.getDate().getTime()));
						r.setPeopleNo(0);
						rsi.addRoomInfo(r);
						JOptionPane.showMessageDialog(null, "成功");
						RoomAddDialog.this.dispose();
					}
				}catch(Exception e){
//						e.printStackTrace();
						JOptionPane.showMessageDialog(null
								, "添加失败，时间格式错误或其他原因");
					}
			}
		});

		this.cancelBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				RoomAddDialog.this.dispose();
			}
		});
		
	}
	
}
